package cn.ac.aircas.web.controller.system;


import cn.ac.aircas.common.annotation.Anonymous;
import cn.ac.aircas.common.core.controller.BaseController;
import cn.ac.aircas.common.core.domain.AjaxResult;
import cn.ac.aircas.common.core.domain.R;
import cn.ac.aircas.system.domain.SysRegion;
import cn.ac.aircas.system.service.ISysRegionService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/region")
@Api(tags = "行政区规划")
public class SysRegionController extends BaseController {

    @Autowired
    private ISysRegionService sysRegionService;

    @GetMapping("/list")
    public R list(SysRegion sysRegion) {
        return R.ok(sysRegionService.selectRegionList(sysRegion));
    }

    @GetMapping("/getRegionDistrict")
//    @Anonymous
    public R getRegionDistrict(SysRegion sysRegion) {
        return R.ok(sysRegionService.getRegionDistrict(sysRegion));
    }

    @GetMapping("/getRegionDistrictByCityName/{cityName}")
//    @Anonymous
    public R getRegionDistrictByCityName(@PathVariable String cityName) {
        return R.ok(sysRegionService.getRegionDistrictByCityName(cityName));
    }

    @GetMapping("/getDistrictBoundary")
//    @Anonymous
    public R getDistrictBoundaryByCityName(@RequestParam String cityName, @RequestParam(required = false) String districtName) {
        return R.ok(sysRegionService.getDistrictBoundaryByCityName(cityName,districtName));
    }

    @PostMapping("/importRegion")
//    @Anonymous
    public R importRegion(@RequestParam("file") MultipartFile file) {
        if (file.isEmpty()) {
            return R.fail("文件为空");
        }

        return R.ok(sysRegionService.importRegion(file));
    }

    /**
     * 查询已上线的城市
     */
    @GetMapping("/getCityList")
    @Anonymous
    public R getCityList(String status) {
        return R.ok(sysRegionService.getCityList(status));
    }

    /**
     * 修改行政区
     */
    @PutMapping("/updateRegion")
    public R updateRegion(@RequestBody SysRegion sysRegion) {
        return R.ok(sysRegionService.updateRegion(sysRegion));
    }

    /**
     * 根据角色id查询行政区
     */
    @GetMapping("/roleRegionTreeSelect/{roleId}")
    public AjaxResult roleRegionTreeSelect(@PathVariable("roleId") Long roleId) {
//        List<SysRegion> regions = sysRegionService.selectRegionList(getUserId());
        AjaxResult ajax = AjaxResult.success();
        ajax.put("checkedKeys", sysRegionService.selectRegionListByRoleId(roleId));
        ajax.put("regions", sysRegionService.getCityList("1"));
        return ajax;
    }
}
